/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.elf.gwt.common.client.model;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONValue;

/**
 *  
 */
public class PaymentModel extends AbstractModel
{
	//{"module":"moneyorder","payment_method":"moneyorder","check_cvv":null,"accepted_credit_cards":null}

	protected String module;
	protected String method;
	protected boolean checkCVV;
	protected String[] acceptedCreditCards;
	protected boolean thirdPartyPaymentProcess;
	protected boolean passCC;
	protected HashMap<String, String> thirdPartyPaymentParameters;
	
	public PaymentModel()
	{
	}

	public PaymentModel(JSONObject jsonObject)
	{
		module = convertToString(jsonObject,"module");
		method = convertToString(jsonObject, "payment_method");
		checkCVV = convertToBoolean(jsonObject, "check_cvv");
		String ccs = convertToString(jsonObject, "accepted_credit_cards");
		acceptedCreditCards = ccs.split(",");
		for (int i=0; i< acceptedCreditCards.length; i++)
		{
			acceptedCreditCards[i] = acceptedCreditCards[i].trim();
		}
		thirdPartyPaymentProcess = convertToBoolean(jsonObject, "third_party_payment_process");
		passCC = convertToBoolean(jsonObject, "pass_cc");
		thirdPartyPaymentParameters = new HashMap<String, String>();
		buildThirdPartyParameters(jsonObject);
	}

	public void update(PaymentModel model)
	{
		module = model.getModule();
		method = model.getMethod();
		checkCVV = model.isCheckCVV();
		acceptedCreditCards = model.getAcceptedCreditCards(); 
		thirdPartyPaymentProcess = model.getThirdPartyPaymentProcess();
		passCC = model.getPassCC();
		thirdPartyPaymentParameters = model.getThirdPartyPaymentParameters();
	}
	
	protected void buildThirdPartyParameters(JSONObject jsonObject)
	{
		//roles.clear();

		try {
			JSONValue paramsValue = jsonObject.get("third_party_payment_parameters");
			if (paramsValue != null)
			{
				JSONObject jsonAObject;
				if ((jsonAObject = paramsValue.isObject()) != null)
				{
					Set<String> keys = jsonAObject.keySet();
					Iterator<String> iter = keys.iterator();
					while (iter.hasNext()) {
						String key = iter.next();
						String param = convertToString(jsonAObject, key);
						thirdPartyPaymentParameters.put(key, param);
					}
					
				}
			}
		}
		catch (Exception e) {}
	}

	public String getModule() {
		return module;
	}

	public String[] getAcceptedCreditCards() {
		return acceptedCreditCards;
	}

	public boolean isCheckCVV() {
		return checkCVV;
	}

	public String getMethod() {
		return method;
	}

	public boolean getThirdPartyPaymentProcess() {
		return thirdPartyPaymentProcess;
	}

	public boolean getPassCC() {
		return passCC;
	}
	
	public HashMap<String, String> getThirdPartyPaymentParameters() {
		return thirdPartyPaymentParameters;
	}
}
